package com.wannengbang.storemobile.homepage.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.bean.DeviceImgListBean;
import com.wannengbang.storemobile.utils.GlideUtils;
import com.wannengbang.storemobile.utils.QRCodeUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class DeviceImgListAdapter extends RecyclerView.Adapter<DeviceImgListAdapter.ViewHolder> {

    private List<DeviceImgListBean.DataBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public DeviceImgListAdapter(List<DeviceImgListBean.DataBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(DeviceImgListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_d0_code, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DeviceImgListBean.DataBean dataBean = listsBeans.get(position);
        if ("3".equals(dataBean.getType())) {
            GlideUtils.loadImage(context, holder.ivCode, dataBean.getUrl());
        } else if ("8".equals(dataBean.getType())) {
            Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(dataBean.getUrl(), 480, 480);
            holder.ivCode.setImageBitmap(mBitmap);
        }
        holder.tvDeviceNo.setText("收款码编号：" + dataBean.getDevice_no());
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_code)
        ImageView ivCode;
        @BindView(R.id.tv_device_no)
        TextView tvDeviceNo;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
